package com.example.childrendapp;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.childrendapp.database.DataBaseHelper;

public class LoadData extends Activity implements OnClickListener {
	ArrayList<Maths> Ma;
	private DataBaseHelper mDB;
	private SQLiteDatabase myDatabase;
	private TextView tv1, tv2, tv3, tv4, tv5;
	private TextView mAnswer1, mAnswer2, mAnswer3, mAnswer4;
	private Cursor mCursor;
	String kq;
	private static int index = 0, rowcount;
	private String TAG = "this is Question value :";
	private boolean checkpass;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.layout_app_startappmath);
		// mMaths=new ArrayList<Maths>();
		mDB = new DataBaseHelper(this);

		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		tv3 = (TextView) findViewById(R.id.tv3);
		tv4 = (TextView) findViewById(R.id.tv4);
		tv5 = (TextView) findViewById(R.id.tv5);
		mAnswer1 = (TextView) findViewById(R.id.answer1);
		mAnswer2 = (TextView) findViewById(R.id.answer2);
		mAnswer3 = (TextView) findViewById(R.id.answer3);
		mAnswer4 = (TextView) findViewById(R.id.answer4);
		mAnswer1.setOnClickListener(this);
		mAnswer2.setOnClickListener(this);
		mAnswer3.setOnClickListener(this);
		mAnswer4.setOnClickListener(this);

		index = 0;
		try {
			mDB.createDataBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mDB.openDataBase();
		try {
			mDB.copyDataBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		myDatabase = mDB.getWritableDatabase();

		loadData();
	}

	@Override
	protected void onResume() {
		loadDataToView(index);
		Log.i(TAG, "Hoai " + mCursor.getCount());
		super.onResume();

	}

	@Override
	public void onClick(View v) {
		if (v == mAnswer1) {
			if (mAnswer1.getText().toString().equals(kq)) {
				Toast.makeText(this, "Pass", Toast.LENGTH_SHORT).show();
				checkpass = true;
				if (index < rowcount - 1) {
					index++;
				} else {
					index = 2;
				}
				RefreshData();
				// Chuyen cau hoi
			} else {
				Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();
			}
		}
		if (v == mAnswer2) {
			if (mAnswer2.getText().toString().equals(kq)) {
				Toast.makeText(this, "Pass", Toast.LENGTH_SHORT).show();
				checkpass = true;
				if (index < rowcount - 1) {
					index++;
				} else {
					index = 2;
				}
				RefreshData();
			} else {
				Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();
			}
		}
		if (v == mAnswer3) {
			if (mAnswer3.getText().toString().equals(kq)) {
				Toast.makeText(this, "Pass", Toast.LENGTH_SHORT).show();
				if (index < rowcount - 1) {
					index++;
				} else {
					index = 2;
				}
				checkpass = true;
				RefreshData();
			} else {
				Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();
			}
		}
		if (v == mAnswer4) {
			if (mAnswer4.getText().toString().equals(kq)) {
				if (index < rowcount - 1) {
					index++;
				} else {
					index = 2;
				}
				Toast.makeText(this, "Pass", Toast.LENGTH_SHORT).show();
				checkpass = true;
				RefreshData();
			} else {
				Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();
			}
		}
	}

	private void RefreshData() {

		// mMaths=new Maths(question, ans1, ans2, ans3, ans4, result);
		tv1.setText("");
		tv2.setText("");
		tv3.setText("");
		tv4.setText("");
		tv5.setText("");
		mAnswer1.setText("");
		mAnswer2.setText("");
		mAnswer3.setText("");
		mAnswer4.setText("");
		loadDataToView(index);
		/*
		 * for(int i=0;i<mCursor.getCount();i++){
		 * 
		 * i++; }
		 */
	}

	private void loadDataToView(int index) {
		Maths item = Ma.get(index);
		char a[] = item.getQuestion().toCharArray();
		tv1.setText("" + a[0]);
		tv2.setText("" + a[1]);
		tv3.setText("" + a[2]);
		tv4.setText("" + a[3]);
		tv5.setText("" + a[4]);
		mAnswer1.setText("" + item.getAnswer1());
		mAnswer2.setText("" + item.getAnswer2());
		mAnswer3.setText("" + item.getAnswer3());
		mAnswer4.setText("" + item.getAnswer4());
		kq = item.getResult();
	}

	private void loadData() {

		// Maths m = new Maths();
		mCursor = myDatabase.rawQuery("SELECT * FROM MATHS", null);
		rowcount = mCursor.getCount();
		if (mCursor != null && mCursor.moveToFirst()) {
			Ma = new ArrayList<Maths>();

			// while(mCursor.isAfterLast()==false){
			for (int i = 0; i < mCursor.getCount(); i++) {
				Maths m = new Maths();
				m.setQuestion(mCursor.getString(mCursor
						.getColumnIndex("Question")));
				m.setAnswer1(mCursor.getString(mCursor
						.getColumnIndex("Answer1")));
				m.setAnswer2(mCursor.getString(mCursor
						.getColumnIndex("Answer2")));
				m.setAnswer3(mCursor.getString(mCursor
						.getColumnIndex("Answer3")));
				m.setAnswer4(mCursor.getString(mCursor
						.getColumnIndex("Answer4")));
				m.setResult(mCursor.getString(mCursor.getColumnIndex("Result")));
				Ma.add(m);
				Log.i(TAG, "hoainhi " + Ma);

				mCursor.moveToNext();
			}

		}

	}
}
